package com.sls.web.manager.game.service.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.GoodsBrand;
import com.sls.web.manager.game.querybean.GoodsBrandQuery;
import com.sls.web.manager.game.service.GoodsBrandService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

@Repository("goodsBrandSerice")
public class GoodsBrandServiceImpl extends SuperService implements
		GoodsBrandService {
	private static Logger log = Logger.getLogger(GoodsBrandServiceImpl.class);
	private Gson gson;

	public GoodsBrandServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean add(GoodsBrand goodsBrand, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter GoodsBrandSerice add method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_BRAND_ADD);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		goodsBrand.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, goodsBrand);
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Leave GoodsBrandSerice add method...");
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean update(GoodsBrand goodsBrand, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter GoodsBrandSerice update method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_BRAND_UPDATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		goodsBrand.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, goodsBrand);
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Leave GoodsBrandSerice update method...");
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<GoodsBrand> queryGoodsBrand(
			GoodsBrandQuery goodsBrandQuery, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter GoodsBrandSerice queryGoodsBrand method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_BRAND_PAGE_QUERY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, goodsBrandQuery);
		String body = dto.getBody();
		Pagination<GoodsBrand> goodsBrand = gson.fromJson(body,
				new TypeToken<Pagination<GoodsBrand>>() {
				}.getType());
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Leave GoodsBrandSerice queryGoodsBrand method...");
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return goodsBrand;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public GoodsBrand queryGoodsBrandById(String id, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter GoodsBrandSerice queryGoodsBrandById method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_BRAND_QUERY_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		GoodsBrand goodsBrand = new GoodsBrand();
		goodsBrand.setGoodsCategoryId(id);
		SocketDTO dto = super.send(head, goodsBrand);
		log.info("服务端返回的信息为：" + dto.getBody());
		GoodsBrand goodsBrand2 = gson.fromJson(dto.getBody(), GoodsBrand.class);
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Leave GoodsBrandSerice queryGoodsBrandById method...");
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return goodsBrand2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean delete(String id, UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter GoodsBrandSerice delete method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_BRAND_DELETE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		GoodsBrand goodsBrand = new GoodsBrand();
		goodsBrand.setId(id);
		goodsBrand.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, goodsBrand);
		log.info("服务端返回的信息为：" + dto.getBody());
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Leave GoodsBrandSerice delete method...");
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<GoodsBrand> queryAllBrand(String state, String enable,
			UserInfoVO userInfoVO) throws Exception {
		GoodsBrandQuery query = new GoodsBrandQuery();
		query.setCurrentPage(1);
		query.setPageSize(20);
		query.setEnable(enable);
		query.setIsDelete(state);

		List<GoodsBrand> brandList = new LinkedList<GoodsBrand>();
		Pagination<GoodsBrand> page = queryGoodsBrand(query, userInfoVO);
		if (page != null) {
			if (page.getList() != null) {
				brandList.addAll(page.getList());
			}
			int count = page.getRowCount();
			if (query.getPageSize() == 0) {
				query.setPageSize(1);
			}
			for (int i = 1; i < Math.ceil(count * 1.0 / query.getPageSize()); i++) {
				query.setCurrentPage(i + 1);
				page = queryGoodsBrand(query, userInfoVO);
				if (page.getList() != null) {
					brandList.addAll(page.getList());
				}
			}
			return brandList;
		}
		return null;
	}

}
